﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Supermarket;

namespace XLSupermarket.Account.Information
{
    public partial class seletInformationForm : Form
    {
        public seletInformationForm()
        {
            InitializeComponent();
        }
        string YPhone = "";
        private void seletInformationForm_Load(object sender, EventArgs e)
        {
            Select();
        }
        /// <summary>
        /// 查询信息
        /// </summary>
        private void Select() {
            string user = LoginForm.UserName_Value;
            string sql = "Select * from EP_information EI,EP_Number EN where EI.EP_ID=EN.EP_ID and EN.UserName='" + user + "'";
            DataTable dt = DBHelper.FillData(sql);
            LB_Id.Text = dt.Rows[0]["EP_ID"].ToString();
            TX_Name.Text = dt.Rows[0]["EP_Name"].ToString();
            LB_Gender.Text = dt.Rows[0]["EP_Gender"].ToString();
            DT_Br.Value = DateTime.Parse(dt.Rows[0]["EP_Birthday"].ToString());
            TB_Addres.Text = dt.Rows[0]["EP_Address"].ToString();
            CB_ST.Text = dt.Rows[0]["Staff_Name"].ToString();
            TB_Phone.Text = dt.Rows[0]["EP_Phone"].ToString();
            YPhone = TB_Phone.Text;
            TB_Src.Text = dt.Rows[0]["Src"].ToString();
            TB_Other.Text = dt.Rows[0]["Other"].ToString();
        }
        /// <summary>
        /// 修改功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (InformationVerification()) return;
            string sql_up_1 = string.Format("UPDATE EP_information SET EP_Name='{0}',EP_Birthday='{1}',EP_Phone='{2}',EP_Address='{3}',Other='{4}' where EP_ID='{5}'",
                                      TX_Name.Text,  DT_Br.Value,  TB_Phone.Text,
                                      TB_Addres.Text, TB_Other.Text, LB_Id.Text);
            string sql_up_2 = string.Format("UPDATE EP_Number SET Src='{0}' where EP_ID = '{1}'",
                                               TB_Src.Text, LB_Id.Text);
            bool result_1 = DBHelper.Edit(sql_up_1) > 0 ? true : false;
            bool result_2 = DBHelper.Edit(sql_up_2) > 0 ? true : false;
            if (result_1) MessageBox.Show("个人信息修改成功！");
            else MessageBox.Show("个人信息修改失败！");
            if (result_2) MessageBox.Show("头像修改成功！");
            else MessageBox.Show("头像修改失败！");
        }
        /// <summary>
        /// 信息验证
        /// </summary>
        /// <returns></returns>
        private bool InformationVerification()
        {
            string datet = DT_Br.Value.ToString("yyyy-MM-dd");
            DateTime date_now = DateTime.Now;
            int age = date_now.Year - DT_Br.Value.Year;
            if (date_now.Month < DT_Br.Value.Month)
            {
                age--;
            }
            else if (date_now.Month == DT_Br.Value.Month && date_now.Day < DT_Br.Value.Day)
            {
                age--;
            }
            if (age < 18)
            {
                MessageBox.Show("本超市不可入职未成年员工！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return true;
            }
            if (TB_Phone.Text.Length != 11)
            {
                DialogResult result_3 = MessageBox.Show("手机号码输入非常规输入！\n号码:" + TB_Phone.Text + " 是否正确？", "提示"
                      , MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result_3 == DialogResult.No)
                {
                    MessageBox.Show("请输入正确的手机号！");
                    return true;
                }
            }
            if (PhoneRepeatedJudgment())
            {
                MessageBox.Show("您的电话号码已存在！");
                return true;
            }
            try
            {
                long a = long.Parse(TB_Phone.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("您的手机号码输入有误！");
                return true;
            }
            return false;
        }
        /// <summary>
        /// 手机号重复验证
        /// </summary>
        /// <returns></returns>
        private bool PhoneRepeatedJudgment()
        {
            if (YPhone== TB_Phone.Text)
            {
                return false;
            }
            string sql = "Select EP_Phone from EP_information ";
            DataTable dt = DBHelper.FillData(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i][0].ToString() == TB_Phone.Text)
                {
                    return true;
                }
            }
            return false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
